#include "kernel_operator.h"
// tensor num for each queue
constexpr int32_t BUFFER_NUM = 2;

template<typename TYPE_X, typename TYPE_Z> class KernelGELU {
    using T = TYPE_X;
public:
    __aicore__ inline KernelGELU() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, uint32_t smallCoreDataNum,
                                uint32_t bigCoreDataNum, uint32_t finalBigTileNum, 
                                uint32_t finalSmallTileNum, uint32_t tileDataNum, 
                                uint32_t smallTailDataNum, uint32_t bigTailDataNum, 
                                uint32_t tailBlockNum) 
    {
        ASSERT(AscendC::GetBlockNum() != 0 && "block dim can not be zero!");
        uint32_t coreNum = AscendC::GetBlockIdx();
        uint32_t globalBufferIndex = bigCoreDataNum * AscendC::GetBlockIdx();
        this->tileDataNum = tileDataNum;
        if (coreNum < tailBlockNum) { 
          this->coreDataNum = bigCoreDataNum;
          this->tileNum = finalBigTileNum;
          this->tailDataNum = bigTailDataNum;
        }
        else { 
          this->coreDataNum = smallCoreDataNum;
          this->tileNum = finalSmallTileNum;
          this->tailDataNum = smallTailDataNum;
          globalBufferIndex -= (bigCoreDataNum - smallCoreDataNum) * (AscendC::GetBlockIdx() - tailBlockNum);
        }
        xGm.SetGlobalBuffer((__gm__ TYPE_X*)x + globalBufferIndex, this->coreDataNum);
        zGm.SetGlobalBuffer((__gm__ TYPE_Z*)z + globalBufferIndex, this->coreDataNum);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileDataNum * sizeof(TYPE_X));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileDataNum * sizeof(TYPE_Z));
        pipe.InitBuffer(tmpBuf0, this->tileDataNum * sizeof(TYPE_X));
        pipe.InitBuffer(tmpBuf1, this->tileDataNum * sizeof(uint8_t));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_X> xLocal = inQueueX.AllocTensor<TYPE_X>();
      AscendC::DataCopy(xLocal, xGm[progress * this->tileDataNum], this->processDataNum);
      inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_X> xLocal = inQueueX.DeQue<TYPE_X>();
      AscendC::LocalTensor<TYPE_Z> zLocal = outQueueZ.AllocTensor<TYPE_Z>();
      AscendC::LocalTensor<TYPE_X> tmpTensor0 = tmpBuf0.Get<TYPE_X>();
      AscendC::LocalTensor<uint8_t> sharedTmpBuffer = tmpBuf1.Get<uint8_t>();
      
      TYPE_X a = 0.7978845608028654f;
      TYPE_X b = 0.044715f;
      TYPE_X one = 1.0f;
      TYPE_X scalar = 0.5f;
      AscendC::Mul(tmpTensor0, xLocal, xLocal, this->processDataNum); // x^2
      // AscendC::DumpTensor(tmpTensor0, 171, 32);
      AscendC::Mul(tmpTensor0, tmpTensor0, xLocal, this->processDataNum); // x^3
      AscendC::Muls(tmpTensor0, tmpTensor0, b, this->processDataNum); // b*x^3
      AscendC::Add(tmpTensor0, tmpTensor0, xLocal, this->processDataNum); // x + b*x^3
      AscendC::Muls(tmpTensor0, tmpTensor0, a, this->processDataNum); // a*(x + b*x^3)
      AscendC::Tanh(tmpTensor0, tmpTensor0, sharedTmpBuffer, this->processDataNum); // tanh(a*(x + b*x^3))
      AscendC::Adds(tmpTensor0, tmpTensor0, one, this->processDataNum); // tanh(a*(x + b*x^3)) + 1
      AscendC::Mul(tmpTensor0, tmpTensor0, xLocal, this->processDataNum); // x*(tanh(a*(x + b*x^3)) + 1)
      AscendC::Muls(zLocal, tmpTensor0, scalar, this->processDataNum); // 0.5*x*(tanh(a*(x + b*x^3)) + 1)

      outQueueZ.EnQue<TYPE_Z>(zLocal);
      inQueueX.FreeTensor(xLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_Z> zLocal = outQueueZ.DeQue<TYPE_Z>();  
      AscendC::DataCopy(zGm[progress * this->tileDataNum], zLocal, this->processDataNum);
      outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<TYPE_X> xGm;
    AscendC::GlobalTensor<TYPE_Z> zGm;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0, tmpBuf1;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};
extern "C" __global__ __aicore__ void gelu_custom(GM_ADDR x, GM_ADDR z, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    KernelGELU<DTYPE_X, DTYPE_Z> op;
    op.Init(x, z, tiling_data.smallCoreDataNum, 
            tiling_data.bigCoreDataNum, tiling_data.finalBigTileNum, 
            tiling_data.finalSmallTileNum, tiling_data.tileDataNum, 
            tiling_data.smallTailDataNum, tiling_data.bigTailDataNum, 
            tiling_data.tailBlockNum);  
    op.Process();
}
